<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <#include "/pubHeadStyleTree.html">
</head>
<body>
<script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/codemirror.js"></script>
<script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/InitMirror.js"></script>
<div style="padding: 10px;height: 86%;overflow: auto;">
    <table class="table-detail" cellpadding="0" cellspacing="0" border="0"  >
        <tr>
            <th>条件表达式</th>
            <td>
                <div style="margin: 8px 0; ">
                    <span id="commonScript" style="cursor: pointer;">
                        <i class="layui-icon layui-icon-fonts-code"></i>常用脚本
                    </span>
                    &nbsp;
                    表单变量:
                    <select name="flowVars" id="flowVars">
                        <option value="">请选择</option>
                    </select>
                    <span id="showSpan" style="display:none;">&nbsp; &nbsp;<input type="checkbox" id="canChoicePath" style="width: 15px;height: 15px;"/>可以选择条件同步路径&nbsp;</span>
                    <select id="canChoicePathNodeId" style="display:none">
                        <#list incomeNodes as inNode>
                        <option value="${inNode.nodeId}">${inNode.nodeName}</option>
                        </#list>
                    </select>
                </div>
                <#list incomeNodes as inNode>
                <div style="padding: 4px;">
                    <#if inNode.isMultiInstance==true>
                    <a href="javascript:;" name="signResult" result='signResult_${inNode.nodeId}=="pass"'>[${inNode.nodeName}]投票通过</a>
                    &nbsp;
                    <a href="javascript:;" name="signResult" result='signResult_${inNode.nodeId}=="refuse"'>[${inNode.nodeName}]投票不通过</a>
                    <#else>
                    <a href="javascript:;" name="signResult" result="approvalStatus_${inNode.nodeId}==1">[${inNode.nodeName}]-通过</a>
                    &nbsp;
                    <a href="javascript:;" name="signResult" result="approvalStatus_${inNode.nodeId}==2">[${inNode.nodeName}]-反对</a>
                    </#if>
                <ul>
                    <li>1.先选中下方的脚本输入框，然后再插入条件表达式。</li>
                    <li>2.脚本为groovy语法。</li>
                    <li>3.这个脚本需要使用返回语句(return)返回布尔值，返回true流程将跳转到指定的节点。</li>
                </ul>
            </div>
            </#list>
            </td>
        </tr>
        <#list outcomeNodes as outNode>
        <tr class="taskTr">
            <th width="20%">
                ${outNode.nodeName }
            </th>
            <td>
                <input type="hidden" name="task" value="${outNode.nodeId}"/>
                <textarea id="${outNode.nodeId}" codemirror="true" mirrorheight="110px" name="condition" rows="3" cols="20" class="inputText">${outNode.condition}</textarea>
            </td>
        </tr>
        </#list>
    </table>
</div>
<div style="text-align: center;position: absolute;bottom: 15px;left: 0;right: 0">
    <button class="layui-btn save">保存</button>
    <button class="layui-btn cancelFrom layui-btn-danger">取消</button>
</div>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','form','jquery','setter','commons'], function () {
        var form = layui.form;
        var $ = layui.$;
        var setter = layui.setter;
        var commons = layui.commons;
        var modelId = '${modelId!}';
        var nodeId = "${nodeId!}";
        var parentProcDefId = "";
        var ifInclusiveGateway = '${ifInclusiveGateway!}';
        var selectCanChoicePathNodeId = '${selectCanChoicePathNodeId!}';
        $(".save").click(saveCondition);
        $("#btnScript").click(selectScript);
        $("a[name='signResult']").click(function() {
            addToTextarea($(this).attr("result"));
        });

        //条件同步分支节点
        if(ifInclusiveGateway=="true"){
            $("#showSpan").attr("style","");
            if(selectCanChoicePathNodeId!=undefined && selectCanChoicePathNodeId!=""){
                $("#canChoicePath").prop("checked",true);
                $("#canChoicePathNodeId").attr("style","");
            }
        }

        $("#canChoicePath").click(function(){
            var targ=$("#canChoicePathNodeId");
            if($(this).prop("checked")){
                targ.attr("style","");
                InitMirror.each(function(e){
                    e.setCode('NextPathId.contains("'+e.targetId+'")');
                });
            }
            else{
                targ.attr("style","display:none");
                InitMirror.each(function(e){
                    e.setCode('');
                });
            }
        });

        $.ajax({
            url: serverPath.workflowPath + "/workflow/bpmDefVar/getFlowVars",
            type: "get",
            data:{modelId:modelId,parentProcDefId:parentProcDefId},
            contentType: 'application/json',
            dataType: 'json',
            success: function(result) {
                var varsMap = result.data;
                var optionHtml="";
                var selectObj = $("#flowVars");
                for(var i=0;i<varsMap.length;i++){
                    optionHtml+="<option value='"+varsMap[i].varKey+"'>"+varsMap[i].varName+"</option>";
                }
                selectObj.append(optionHtml);
            }
        });

        //将条件表达式追加到脚本输入框内
        function addToTextarea(str){
            InitMirror.editor.insertCode(str);
        };

        $("#flowVars").change(function(){
            addToTextarea($(this).val());
        });

        function saveCondition() {
            InitMirror.save();
            var tasks = [];
            var conditions = [];
            $("tr.taskTr > td").each(function(){
                var condition=$("[name='condition']", $(this)).val();
                var task=$("[name='task']", $(this)).val();
                tasks.push(task);
                conditions.push(condition);
            });

            var canChoicePathNodeId=$("#canChoicePathNodeId:visible").val();

            var url = "${ctx}/workflow/actDefModel/addCondition";
            var paras = {
                "modelId" : modelId,
                "nodeId" : nodeId,
                "tasks" : tasks.join('#split#'),
                "conditions" : conditions.join('#split#'),
                "canChoicePathNodeId":canChoicePathNodeId
            };
            $.ajax({
                url: url,
                type: "POST",
                data: paras,
                dataType:"json",
                success: function (data) {
                    if (commons._filterRecord(data)) {
                        window.parent.layer.alert(data.msg, function (index) {
                            parent.layer.close(index);
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                        });
                    } else {
                        window.parent.layer.alert(data.msg);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.alert("保存失败！" + XMLHttpRequest.status);
                },
            });
        };

        function selectScript() {
            ScriptDialog({
                callback : function(script) {
                    addToTextarea(script);
                }
            });
        };
        $("#commonScriptId").click(function(){
            layer.open({
                type: 2,
                area: ['75%', '100%'],
                title: ['脚本'],
                content: serverPath.formPath + setter.formLinkPath + "table/view/script?param=2"
            });
        });

        $(".cancelFrom").click(function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name))
        });

        $("#commonScript").click(function(){
            layer.open({
                type: 2,
                area: ['100%', '100%'],
                title: ['脚本'],
                content: serverPath.formPath + setter.formLinkPath + "table/view/script?param=3"
            });
        });

    });
</script>
</body>
</html>

